import sys
import file_manager
import student_manager
import model
import tools


def register():  # 注册
    data = file_manager.read_json('teacher.json')
    # 输入账号密码
    while True:
        teacher_name = input('please enter your name(3-6):')
        if not 3 <= len(teacher_name) <= 6:
            print('unavailable name, please enter again.')
        else:
            break
    if teacher_name in data:
        print('username is already registered')
        return
    while True:
        password = input('please enter your password(6-12)')
        if not 6 <= len(password) <= 12:
            print('unavailable password, please enter again.')
        else:
            break
    # 存储老师用户
    t = model.Teacher(teacher_name, password)
    data[t.name] = t.password
    file_manager.write_json('teacher.json', data)


def login():
    # 读文件
    data = file_manager.read_json('teacher.json')
    # 输入并验证账号
    teacher_name = input('please enter your name:')
    if teacher_name not in data:
        print('this name is not registered!')
        return
    # 输入并验证密码
    password = input('please enter your password:')
    while tools.encrypt_password(password) != data[teacher_name]:
        print('wrong password')
        password = input('please enter your password again:')
    # 进入管理页面
    student_manager.teacher_name = teacher_name
    student_manager.show_page()


def start():
    content = file_manager.read_file('welcome.txt')
    while True:
        operator = input(content)
        if operator == '1':
            register()
        elif operator == '2':
            login()
        elif operator == '3':
            # break
            # exit(0)
            sys.exit(0)
        else:
            print('please enter number 1-3')


if __name__ == '__main__':
    start()
